const LRUCache = require("../template")

function testLRUCacheComplexOperations() {
  console.log(
    "1. 大量操作测试:",
    (() => {
      const cache = new LRUCache(5)
      for (let i = 1; i <= 10; i++) {
        cache.put(i, `value${i}`)
      }
      const lastFive = Array.from({ length: 5 }, (_, i) => i + 6).every(i => cache.get(i) === `value${i}`)
      const firstFive = Array.from({ length: 5 }, (_, i) => i + 1).every(i => cache.get(i) === -1)
      return lastFive && firstFive
    })()
  )

  console.log(
    "2. 频繁访问和更新测试:",
    (() => {
      const cache = new LRUCache(3)
      cache.put(1, "a")
      cache.put(2, "b")
      cache.put(3, "c")
      // 频繁访问 1
      for (let i = 0; i < 5; i++) {
        cache.get(1)
      }
      cache.put(4, "d") // 应该移除 2 (最久未使用)
      return cache.get(1) === "a" &&
             cache.get(2) === -1 &&
             cache.get(3) === "c" &&
             cache.get(4) === "d"
    })()
  )

  console.log(
    "3. 混合操作测试:",
    (() => {
      const cache = new LRUCache(4)
      cache.put(1, "a")
      cache.put(2, "b")
      cache.get(1) // 访问 1
      cache.put(3, "c")
      cache.put(4, "d")
      cache.get(2) // 访问 2
      cache.put(5, "e") // 应该移除 3 (最久未使用)
      return cache.get(1) === "a" &&
             cache.get(2) === "b" &&
             cache.get(3) === -1 &&
             cache.get(4) === "d" &&
             cache.get(5) === "e"
    })()
  )

  console.log(
    "4. 清空后重新使用测试:",
    (() => {
      const cache = new LRUCache(2)
      cache.put(1, "a")
      cache.put(2, "b")
      const sizeBefore = cache.size()
      cache.clear()
      const sizeAfter = cache.size()
      cache.put(3, "c")
      cache.put(4, "d")
      const get3 = cache.get(3)
      const get4 = cache.get(4)
      return sizeBefore === 2 &&
             sizeAfter === 0 &&
             get3 === "c" &&
             get4 === "d"
    })()
  )

  console.log(
    "5. 性能测试:",
    (() => {
      const cache = new LRUCache(100)
      const start = performance.now()
      for (let i = 1; i <= 200; i++) {
        cache.put(i, `value${i}`)
      }
      for (let i = 101; i <= 200; i++) {
        cache.get(i)
      }
      const end = performance.now()
      return (end - start) < 1000 // 性能测试应该在1秒内完成
    })()
  )

  console.log(
    "6. 复杂访问模式测试:",
    (() => {
      const cache = new LRUCache(3)
      cache.put(1, "a")
      cache.put(2, "b")
      cache.put(3, "c")
      cache.get(2) // 访问 2
      cache.get(1) // 访问 1
      cache.put(4, "d") // 应该移除 3
      cache.get(3) // 访问 3 (已被移除)
      cache.put(5, "e") // 应该移除 2
      return cache.get(1) === "a" &&
             cache.get(2) === -1 &&
             cache.get(3) === -1 &&
             cache.get(4) === "d" &&
             cache.get(5) === "e"
    })()
  )

  console.log(
    "7. this上下文复杂操作测试:",
    (() => {
      const complexObj = {
        multiplier: 2,
        createCache: function(capacity) {
          const cache = new LRUCache(capacity * this.multiplier)
          for (let i = 1; i <= 5; i++) {
            cache.put(i, `value${i}`)
          }
          cache.get(1) // 访问 1
          cache.put(6, "value6") // 应该移除 2
          return cache.get(2) === -1 && cache.get(6) === "value6"
        }
      }
      return complexObj.createCache(2) // 容量 4
    })()
  )

  console.log(
    "8. 复杂对象this绑定复杂操作测试:",
    (() => {
      const complexConfigObj = {
        config: { operations: 10 },
        createConfigCache: function() {
          const cache = new LRUCache(5)
          for (let i = 1; i <= this.config.operations; i++) {
            cache.put(i, `value${i}`)
          }
          const lastFive = Array.from({ length: 5 }, (_, i) => i + 6).every(i => cache.get(i) === `value${i}`)
          const firstFive = Array.from({ length: 5 }, (_, i) => i + 1).every(i => cache.get(i) === -1)
          return lastFive && firstFive
        }
      }
      return complexConfigObj.createConfigCache()
    })()
  )

  console.log(
    "9. 复杂操作一致性测试:",
    (() => {
      const cache = new LRUCache(3)
      cache.put(1, "a")
      cache.put(2, "b")
      cache.put(3, "c")
      cache.put(4, "d") // 移除 1
      cache.put(5, "e") // 移除 2
      cache.put(6, "f") // 移除 3
      cache.get(4) // 访问 4
      cache.put(7, "g") // 移除 5
      return cache.get(4) === "d" &&
             cache.get(5) === -1 &&
             cache.get(6) === "f" &&
             cache.get(7) === "g"
    })()
  )

  console.log(
    "10. 复杂操作边界测试:",
    (() => {
      const cache = new LRUCache(2)
      cache.put(1, "a")
      cache.put(2, "b")
      cache.put(1, "aa") // 更新 1
      cache.put(3, "c") // 移除 2
      cache.put(2, "bb") // 移除 1
      cache.put(4, "d") // 移除 3
      return cache.get(1) === -1 &&
             cache.get(2) === "bb" &&
             cache.get(3) === -1 &&
             cache.get(4) === "d"
    })()
  )
}

testLRUCacheComplexOperations()
